*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


***********************************************
*Rolling regressions

***********************************************

foreach u in "czonestate" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'

***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************


gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm


**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')


*Weighting

forval j=1990/2000 {
gen emp`j'=emp if year==`j'
gen pop`j'=workagepop if year==`j'
if "`u'" == "czonestate" {
bys sic87xx czone state: egen emp`j'_=mean(emp`j')
bys sic87xx czone state: egen pop`j'_=mean(pop`j')
	}
else {
bys sic87xx county: egen emp`j'_=mean(emp`j')
bys sic87xx county: egen pop`j'_=mean(pop`j')
}
drop emp`j' pop`j'
ren emp`j'_ emp`j'
ren pop`j'_ pop`j'
gen lemp`j'=log(emp`j')
gen lpop`j'=log(pop`j')

}


*Windows

forval j=1990/2000 {
local awemp`j' "[aw = emp`j']"
local awpop`j' "[aw = pop`j']"
}

*START IN 1990 OR 1993
foreach j in "1990" "1993" {
local window`j' "`period'>=`j' & `period'<=2016"

*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"

*Specifications

foreach h in "emp" "wage" {

local controls "`tot`h'var' `popvar'"
local entityfe "i.`u'"

eststo l`j'_0_`h': reghdfe ``h'var' `mwvar' `controls' if (`window`j''), `options'

eststo l`j'_1_`h': reghdfe ``h'var' `mwvar' `controls' if (`window`j'') `awemp`j'', `options'

eststo l`j'_2_`h': reghdfe ``h'var' `mwvar' `controls' if (`window`j'') `awpop`j'', `options'



}

}

}

/* Print results */

tempfile specs

#delimit ;
esttab l1990* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("1990-2016")
	keep(`mwvar')
	nonotes
	label
	noobs	
	replace;
#delimit cr

#delimit ;
esttab l1993* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("1993-2016")
	keep(`mwvar')
	nonotes
	label
	noobs	
	append;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(Table6)


clear all



